package cake.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cake.dao.AdminDao;

import cake.dao.impl.AdminDaoImpl;

import cake.entity.Admin;

@WebServlet("/admin/adminLogin")
public class AdminLoginServlet extends HttpServlet{
	
	private AdminDao adminDao=new AdminDaoImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getSession().removeAttribute("LoginAdmin");
		resp.sendRedirect("adminLogin.jsp");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String userName=req.getParameter("username");
		String passWord=req.getParameter("password");
		Admin admin=new Admin();
		admin=adminDao.checkLoginAdmin(userName, passWord);
		if (admin!= null && admin.isStatus()) {
			req.getSession().setAttribute("LoginAdmin", admin);
			resp.sendRedirect("userlist.jsp");
		} else {
			req.setAttribute("error", "用户名或密码错误");
			req.getRequestDispatcher("adminLogin.jsp").forward(req, resp);
		}
	}
}
